package cn.meibang.common.beauty.provider.dao;

import cn.meibang.common.beauty.provider.domain.BeautyConsultationOrderUserProduct;
import cn.meibang.common.beauty.provider.request.ListRecommendHistoryRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SqlBuilder;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class BeautyConsultationOrderUserProductSqlProvider {

    public String listProduct(ListRecommendHistoryRequest request) {
        BEGIN();
        getSelected();
        FROM("beauty_consultation_order_user_product a ");
        getComonJoin(request);
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        sql.append(" limit " + (request.getPageRecord().getCurrentRecord() - request.getPageRecord().getPageSize()) + " ,  "
                + request.getPageRecord().getPageSize());
        sql.append("order by a.create_date");
        return sql.toString();
    }

    /**
     * 查询条件
     *
     * @param record
     */
    public void getComonJoin(ListRecommendHistoryRequest record) {

        if (record.getId() != null) {
            SqlBuilder.WHERE(" a.id =#{id}");
        }

        if (record.getUserId() != null) {
            SqlBuilder.WHERE(" a.user_id =#{userId}");
        }

        if (StringUtils.isNotEmpty(record.getOpenid())) {
            SqlBuilder.WHERE(" a.openid =#{openid}");
        }

        if (StringUtils.isNotEmpty(record.getRecommendType())) {
            SqlBuilder.WHERE(" a.recommend_type =#{recommendType}");
        }
    }

    public void getSelected() {
        SqlBuilder
                .SELECT("a.id, a.tag, a.user_id as userId, openid, a.order_id as orderId, a.recommend_type as recommendType, a.product_id as productId, a.product_name as productName, a.consultant_id as consultantId,a.consultant_name as consultantName, a.price, a.create_by as createBy, a.create_date as createDate, a.update_by as updateBy, a.update_date as updateDate, a.remarks, a.del_flag as delFlag, a.institution_id as institutionId");
    }

    public String insertSelective(BeautyConsultationOrderUserProduct record) {
        BEGIN();
        INSERT_INTO("beauty_consultation_order_user_product");

        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }

        if (record.getTag() != null) {
            VALUES("tag", "#{tag,jdbcType=VARCHAR}");
        }

        if (record.getUserId() != null) {
            VALUES("user_id", "#{userId,jdbcType=BIGINT}");
        }

        if (record.getOpenid() != null) {
            VALUES("openid", "#{openid}");
        }

        if (record.getOrderId() != null) {
            VALUES("order_id", "#{orderId,jdbcType=BIGINT}");
        }

        if (record.getRecommendType() != null) {
            VALUES("recommend_type", "#{recommendType,jdbcType=VARCHAR}");
        }

        if (record.getProductId() != null) {
            VALUES("product_id", "#{productId,jdbcType=BIGINT}");
        }

        if (record.getProductName() != null) {
            VALUES("product_name", "#{productName,jdbcType=VARCHAR}");
        }

        if (record.getConsultantId() != null) {
            VALUES("consultant_id", "#{consultantId,jdbcType=BIGINT}");
        }

        if (record.getConsultantName() != null) {
            VALUES("consultant_name", "#{consultantName,jdbcType=VARCHAR}");
        }

        if (record.getPrice() != null) {
            VALUES("price", "#{price,jdbcType=DECIMAL}");
        }

        if (record.getCreateBy() != null) {
            VALUES("create_by", "#{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            VALUES("create_date", "#{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            VALUES("update_by", "#{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            VALUES("update_date", "#{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            VALUES("remarks", "#{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            VALUES("del_flag", "#{delFlag,jdbcType=CHAR}");
        }

        if (record.getInstitutionId() != null) {
            VALUES("institution_id", "#{institutionId,jdbcType=BIGINT}");
        }

        return SQL();
    }

    public String updateByPrimaryKeySelective(BeautyConsultationOrderUserProduct record) {
        BEGIN();
        UPDATE("beauty_consultation_order_user_product");

        if (record.getTag() != null) {
            SET("tag = #{tag,jdbcType=VARCHAR}");
        }

        if (record.getUserId() != null) {
            SET("user_id = #{userId,jdbcType=BIGINT}");
        }

        if (record.getOpenid() != null) {
            SET("openid = #{openid}");
        }

        if (record.getOrderId() != null) {
            SET("order_id = #{orderId,jdbcType=BIGINT}");
        }

        if (record.getRecommendType() != null) {
            SET("recommend_type = #{recommendType,jdbcType=VARCHAR}");
        }

        if (record.getProductId() != null) {
            SET("product_id = #{productId,jdbcType=BIGINT}");
        }

        if (record.getProductName() != null) {
            SET("product_name = #{productName,jdbcType=VARCHAR}");
        }

        if (record.getConsultantId() != null) {
            SET("consultant_id = #{consultantId,jdbcType=BIGINT}");
        }

        if (record.getConsultantName() != null) {
            SET("consultant_name = #{consultantName,jdbcType=VARCHAR}");
        }

        if (record.getPrice() != null) {
            SET("price = #{price,jdbcType=DECIMAL}");
        }

        if (record.getCreateBy() != null) {
            SET("create_by = #{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            SET("create_date = #{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            SET("update_by = #{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            SET("update_date = #{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            SET("remarks = #{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            SET("del_flag = #{delFlag,jdbcType=CHAR}");
        }

        if (record.getInstitutionId() != null) {
            SET("institution_id = #{institutionId,jdbcType=BIGINT}");
        }

        WHERE("id = #{id,jdbcType=BIGINT}");

        return SQL();
    }
}